package com.ruan.service;

import com.ruan.dao.GoodsMapper;
import com.ruan.dao.GoodsTypeMapper;
import com.ruan.entity.GoodsType;
import com.ruan.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName:GoodsTypeService
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/6/30 23:46
 **/
@Service
public class GoodsTypeService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    public Result addGoodsType(String goodsType) {
        GoodsType goodsType1 = goodsTypeMapper.getGoodsTypeByName(goodsType);
        if(goodsType1!=null){
            return new Result(100,"改商品分类已存在");
        }
        int row = goodsTypeMapper.addGoodsType(goodsType);
        if(row!=1){
            return new Result(100,"添加失败");
        }else {
            return new Result(200,"添加成功");
        }
    }

    public Result getGoodsTypeList() {
        List<GoodsType> goodsTypeList = goodsTypeMapper.getGoodsTypeList();
        return new Result(200,goodsTypeList);
    }

    public Result editGoodsType(GoodsType goodsType) {
        GoodsType goodsType1 = goodsTypeMapper.getGoodsTypeByName(goodsType.getGoodsType());
        if(goodsType1!=null){
            return new Result(100,"改商品分类已存在");
        }
        int row = goodsTypeMapper.editGoodsType(goodsType);
        if(row!=1){
            return new Result(100,"修改失败");
        }else {
            return new Result(200,"修改成功");
        }
    }

    @Transactional
    public Result deleteGoodsType(Integer id) {
        int row = goodsTypeMapper.deleteGoodsType(id);
        goodsMapper.deleteGoodsByTypeId(id);
        if(row!=1){
            return new Result(100,"删除失败");
        }else {
            return new Result(200,"删除成功");
        }
    }
}
